<template>
  <div class="login_container">
	  <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="">
      </div>
      <el-form label-width="0" :model="Loginform" class="login_form" :rules="LoginFormRules" ref="LoginRef">
        <!-- 用户名 -->
     <el-form-item   prop="username">
    <el-input prefix-icon="el-icon-s-custom" v-model="Loginform.username"  ></el-input>
     </el-form-item >
     <!-- 密码框 -->
     <el-form-item prop="password">
    <el-input prefix-icon="el-icon-lock" v-model="Loginform.password" type="password" ></el-input>
     </el-form-item >
     <!-- 按钮区 -->
      <el-form-item class="btns">
        <el-button type="primary" @click="login()">登陆</el-button>
       <el-button type="info" @click="reset()">重置</el-button>
       </el-form-item>
     </el-form>
    </div>
  </div>
</template>

<script>
export default {
name:'Login',
data() {
  return {
    Loginform:{
      username:'admin',
      password:'123456'
    },
   
    // 表单验证
LoginFormRules:{
  // 验证用户规则
username:[
    {required: true, message: '请输入登录名', trigger: 'blur' },
     { min: 3, max: 5, message: '长度在 3 到 5 个字用户名', trigger: 'blur'}
],
password:[
   {required: true, message: '请输入密码', trigger: 'blur'},
  { min: 6, max: 15, message: '长度在 6 到 15位密码', trigger: 'blur' }]
}
  }
},
methods: {
 login(){
   this.$refs.LoginRef.validate( async valid=>{
     if(!valid) return
    const {data:res}= await this.$http.post("login",this.Loginform)
     if(res.meta.status!==200) return this.$message.error("登录失败!")
    this.$message.success("登陆成功!")
    
    //将token存到session里
    window.sessionStorage.setItem('token',res.data.token)
    //跳转到home页
    this.$router.push('/home')
  
   })

 },
 reset(){
   this.$refs.LoginRef.resetFields()
 }

  
},
}
</script>

<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100vh;
}
.login_box{
  background-color: #fff;
  border-radius: 3px;
  height: 300px;
  width: 450px;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;

  .avatar_box{
 height: 130px;
 width: 130px;
 border-radius: 50%;
 border: 1px solid #eee;
 padding: 10px;
 box-shadow: 0 0 10px #ddd;
 position: absolute;
 left: 50%;
 transform: translate(-50%,-50%);
 background-color: #fff;
img{
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
 

}

 
  }

}
.btns{
  display: flex;
  justify-content: flex-end;
}
.login_form{
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>